package mumu.android.app;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.util.Random;

import mumu.android.R;
import mumu.android.lib.tool.MLog;
import mumu.android.lib.tool.Tools;
import android.app.Activity;
import android.content.res.AssetManager.AssetInputStream;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.os.Bundle;
import android.widget.ImageView;

/**
 * 
 * bind服务处理。 关闭activity后，服务终止
 * 
 * @author Administrator
 * 
 */
public class fileEncry3 extends Activity {

	public static String OriginalFile = Tools.getSDPath() + "/hsy/unzipfile/1/Advertisement/1.png";
	public static String EncryFile = Tools.getSDPath() + "/hsy/unzipfile/1/Advertisement/11.png";
	public static String descryFile = Tools.getSDPath() + "/hsy/unzipfile/1/Advertisement/111.png";

	public static final int KEY = 0X38;// 异或加密key

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fileencry);
		// 加密
		encry();
		// 解密
		Bitmap bm = decry();
		ImageView img_1 = (ImageView) findViewById(R.id.img_1);
		if (bm != null)
			img_1.setImageBitmap(bm);

	}

	public Bitmap decry() {
		FileInputStream ofis = null;
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		try {
			File file = new File(EncryFile);
			ofis = new FileInputStream(file);
			byte[] xorIndex = new byte[1];
			// 取出第一个字节，判断异或字节数
			ofis.read(xorIndex);

			byte[] buffer = new byte[1024];
			int l = 0;
			long count = 0;
			while ((l = ofis.read(buffer)) >= 0) {
				count += l;
				if (count <= 1024) {
					for (int i = 0; i < buffer.length; i++) {
						if (i <= xorIndex[0]) {
							buffer[i] = (byte) (buffer[i] ^ KEY);// 异或处理，还原数据
						}
					}
				}
				outputStream.write(buffer);
			}
			return BitmapFactory.decodeByteArray(outputStream.toByteArray(), 0, outputStream.size());

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (ofis != null)
					ofis.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	// 生成随机数
	public int rd(int max, int min) {
		Random random = new Random();

		return random.nextInt(max - min) + min;
	}

	public void encry() {
		FileInputStream ofis = null;
		FileOutputStream efos = null;
		try {
			File efile = new File(EncryFile);
			if (!efile.exists()) {
				efile.createNewFile();
			} else {
				efile.delete();
			}
			File ofile = new File(OriginalFile);
			long filelength = ofile.length();
			ofis = new FileInputStream(ofile);
			byte[] buffer = new byte[1024];
			int l = 0;
			efos = new FileOutputStream(efile);

			// 计算异或的数据量
			int max = (filelength > 100 ? 100 : (int) filelength);
			int min = (filelength > 50 ? 50 : (int) filelength);
			// 生成一个随机数写入数据流，用于判断需要取多少个字节进行异或处理
			int xorIndex = rd(max, min);
			efos.write(xorIndex);

			long count = 0;
			while ((l = ofis.read(buffer)) >= 0) {
				count += l;
				if (count <= 1024) {
					for (int i = 0; i < buffer.length; i++) {
						if (i <= xorIndex) {
							int b = buffer[i] ^ KEY;// 异或处理，加密数据
							buffer[i] = (byte) (b);
						}
					}
				}
				efos.write(buffer);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (ofis != null)
					ofis.close();
				if (efos != null)
					efos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

}
